<?php
/**
 * @title 1349. 参加考试的最大学生数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $seats
     * @return Integer
     */
    function maxStudents($seats) {
        /**
         * @since 2023-12-26 取出有效座位
         */
        $validSeatArray = [];

        /**
         * @since 2023-12-26 座位行数和列数
         */
        $seatRows = count($seats);
        $seatCols = count($seats[0]);

        /**
         * @since 2023-12-26 遍历座位，取出有效座位
         */
        for($r=0; $r<$seatRows; $r++){
            for($c=0; $c<$seatCols; $c++){
                if($seats[$r][$c] == '.'){
                    $validSeatArray["{$r}-{$c}"] = [$r, $c];
                }
            }
        }
        // return $validSeatArray;

        /**
         * @since 2023-12-26 避免重复计算
         */
        $this->dpArray = [];

        /**
         * @since 2023-12-26 可容纳最大学生数
         */
        return $this->MaxStudentCount($validSeatArray);
    }

    /**
     * @title 可容纳最大学生数
     * @author start2004
     * @since 2023-12-26 Created
     *
     * @param Array $validSeatArray
     * @return Int
     */
    function MaxStudentCount($validSeatArray){
        /**
         * @since 2023-12-26 数组为空
         */
        if(empty($validSeatArray)){
            return 0;
        } else {}

        /**
         * @since 2023-12-26 缓存命中
         */
        $key = implode("#", array_keys($validSeatArray));
        if(isset($this->dpArray[$key])) {
            return $this->dpArray[$key];
        } else {}

        /**
         * @since 2023-12-26 取出一个座位
         */
        list($r, $c) = array_pop($validSeatArray);

        /**
         * @since 2023-12-26 该座位不坐学生
         */
        $maxStudentCount1 = $this->MaxStudentCount($validSeatArray);

        /**
         * @since 2023-12-26 该座位坐学生
         * @since 2023-12-26 该座位的左上角、右上角、左边、右边不能坐学生
         */
        $notSeatArray = [
            [$r-1, $c-1],   // 左上角
            [$r-1, $c+1],   // 右上角
            [$r, $c-1],     // 左边
            [$r, $c+1],     // 右边
        ];

        /**
         * @since 2023-12-26 剔除这些座位
         */
        foreach ($notSeatArray as list($notR, $notC)){
            unset($validSeatArray["{$notR}-{$notC}"]);
        }
        $maxStudentCount2 = 1 + $this->MaxStudentCount($validSeatArray);

        // echo "r:{$r}, c:{$c}, maxStudentCount1:{$maxStudentCount1}, maxStudentCount2:{$maxStudentCount2}\n";
        // echo "validSeatArray:". implode(",", array_keys($validSeatArray)) ."\n\n\n\n";

        /**
         * @return
         */
        return $this->dpArray[$key] = max($maxStudentCount1, $maxStudentCount2);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.26.php
 */
$datas = [
    [[["#",".","#","#",".","#"],
        [".","#","#","#","#","."],
        ["#",".","#","#",".","#"]]], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maxStudents(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
